package com.xhsj.user.hr.process.processDefinition.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import com.xhsj.user.hr.process.hrLeaveProcess.query.WorkFlowNodeQuery;
import com.xhsj.user.hr.process.processDefinition.entity.TbGlobalWorkFlowNode;
import com.xhsj.user.hr.process.processDefinition.service.TbGlobalWorkFlowNodeService;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.springframework.security.access.prepost.PreAuthorize;


/**
 * <p>
 * 流程设计-流程节点 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Aug 28, 2020 4:40:10 PM
 */
@Slf4j
@Api(tags = "流程设计-流程节点")
@RestController
@RequestMapping("/process/tbGlobalWorkFlowNode")
public class TbGlobalWorkFlowNodeController {

	@Autowired
    public TbGlobalWorkFlowNodeService service;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:40:10 PM
	 */
	@ApiOperation(value = "流程设计-流程节点,保存/修改", notes = "流程设计-流程节点,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:global:work:flow:node:edit')")
	public Message save(@RequestBody TbGlobalWorkFlowNode data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:40:10 PM
	 */
	@ApiOperation(value = "流程设计-流程节点删除", notes = "通过id删除流程设计-流程节点信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:work:flow:node:delete')")
	public Message delete(@RequestParam(value="id") String id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:40:10 PM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public Message batchDelete(@RequestBody List<String> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Aug 28, 2020 4:40:10 PM
    */
	@ApiOperation(value = "分页查询流程设计-流程节点list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:global:work:flow:node:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbGlobalWorkFlowNode> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}


	@ApiOperation(value = "查询流程节点列表", notes = "查询流程节点列表")
	@RequestMapping(value="/findNodeList",method = RequestMethod.GET)
	public Message findNodeList(@RequestParam("businessId") Long businessId,@RequestParam("dataId") Long dataId) {

		try {
			List<WorkFlowNodeQuery> workFlowNodeQueries = service.nodeList(businessId);
			Long nodeId = service.findCurrentNodeId(businessId,dataId);
			Map<String, Object> map = Maps.newHashMap();
			map.put("currentNodeId", nodeId);
			map.put("nodeList", workFlowNodeQueries);

			return Message.success("成功",map);
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"异常");
		}

	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Aug 28, 2020 4:40:10 PM
    */
	@ApiOperation(value = "查询流程设计-流程节点所有数据list", notes = "查询流程设计-流程节点所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:global:work:flow:node:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Aug 28, 2020 4:40:10 PM
    */
	@ApiOperation(value = "通过id获取流程设计-流程节点实体类", notes = "通过id获取流程设计-流程节点实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:work:flow:node:view')")
	public Message getById(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author liuqinghua
    * @date Aug 28, 2020 4:40:10 PM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:work:flow:node:view')")
	public Message detail(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

}

